from flask import render_template, request
from app import app
import re
import requests
import os

@app.route('/')
def index():
    return render_template("index.html")
@app.route('/update', methods = ['GET', 'POST'])
def update():
    if request.method == 'POST':
        post = request.form.to_dict(True)
        url = post['url']
        website = re.search('//(.*?)/',url,re.S).group(1)
        numberlist = list()
        f = open('app/static/'+website,'r')
        for line in f.readlines():
            numberlist.append(line)
        f.close()
        numberlist = handle(numberlist, post)
        return render_template("numbers.html",numberlist=numberlist)
    else:
        return render_template("numbers.html",numberlist=numberlist)
@app.route('/crawlurl', methods = ['GET', 'POST'])
def crawlurl():
    numberlist = list()
    url = ''
    post = request.form.to_dict(True)
    if request.method == 'POST':
        url = post['url']
    website = re.search('//(.*?)/',url,re.S).group(1)
    if os.path.exists('app/static/'+website):
        f = open('app/static/'+website,'r')
        for line in f.readlines():
            numberlist.append(line)
        f.close()
        return render_template("numbers.html",numberlist=numberlist)
    for i in range(1,101):
        page = re.sub('=(\d+)&','=%d&'%i,url,re.S)
        page = re.sub('=(\d+$)','=%d'%i,page,re.S)
        html = requests.get(page)
        templist = re.findall('\d{11}', html.text, re.S)
        numberlist.extend(list(set(templist)))
    f = open('app/static/'+website,'w')
    for l in numberlist:
        f.write(l+'\n')
    f.close()
    return render_template("numbers.html",numberlist=numberlist)
def handle(numberlist, post):
    if 'with' in post:
        app.wi = post['with']
        numberlist = filter(_wi, numberlist)
    if 'notwith' in post:
        app.wi = post['with']
        app.notwith = post['notwith']
        numberlist = filter(_notwith, numberlist)
    if 'with2[first]' in post:
        app.with2first = post['with2[first]']
        app.with2second = post['with2[second]']
        numberlist = filter(_with2, numberlist)
    if 'poswith[pos]' in post:
        app.poswithpos = post['poswith[pos]']
        app.poswithval = post['poswith[value]']
        numberlist = filter(_poswith, numberlist)
    return numberlist
def _wi(l):
    if re.findall(app.wi, l):
        return True
    else:
        return False
def _notwith(l):
    if re.findall(app.notwith, l):
        return False
    else:
        return True
def _with2(l):
    if re.findall(app.with2first, l) and re.findall(app.with2second, l):
        return False
    else:
        return True
def _poswith(l):
    if l[int(app.poswithpos)-1] == app.poswithval:
        return True
    else:
        return False

